<template>
  <div class="app-container">
    <div class="filter-container">
      <el-card>
        <el-form ref="searchform" :inline="true" :model="searchform" class="demo-form-inline" size="small">
          <el-form-item label="优惠劵名称：">
            <el-input v-model="searchform.Keywords" clearable placeholder="优惠劵名称" />
          </el-form-item>
          <el-form-item label="用户昵称：">
            <el-input v-model="searchform.NickName" clearable placeholder="用户昵称" />
          </el-form-item>
          <el-form-item label="状态：">
            <el-select v-model="searchform.Status" placeholder="请选择">
              <el-option
                v-for="item in StatusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch()">查询</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
    <el-card>
      <el-table
        ref="gridtable"
        v-loading="tableloading"
        :data="tableData"
        border
        stripe
        highlight-current-row
        style="width: 100%"
        :default-sort="{prop: 'SortCode', order: 'descending'}"
        @select="handleSelectChange"
        @select-all="handleSelectAllChange"
        @sort-change="handleSortChange"
      >
        <el-table-column
          type="selection"
          width="30"
        />
        <el-table-column
          prop="NickName"
          label="用户昵称"
          sortable="custom"
          width="230"
          fixed
        />
        <el-table-column
          prop="Code"
          label="兑换码"
          sortable="custom"
          width="230"
          fixed
        />
        <el-table-column
          prop="CouponDto.TypeDes"
          label="类型"
          width="230"
          fixed
        />
        <el-table-column
          prop="CouponDto.Title"
          label="优惠劵名称"
          width="200"
          fixed
        />
        <el-table-column
          prop="StatusDes"
          label="状态"
          width="200"
          fixed
        />
        <el-table-column
          prop="UseTime"
          label="使用时间"
          sortable="custom"
          width="200"
          fixed
        />
        <el-table-column
          prop="ReceiveTime"
          label="领取时间"
          sortable="custom"
          width="200"
          fixed
        />
        <el-table-column
          prop="OrderNo"
          label="关联订单号"
          width="180"
          sortable
        />
      </el-table>
      <div class="pagination-container">
        <el-pagination
          background
          :current-page="pagination.currentPage"
          :page-sizes="[5,10,20,50,100, 200, 300, 400]"
          :page-size="pagination.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.pageTotal"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>

import { getLogPage } from '@/api/shop/couponservice'
import * as fecha from 'element-ui/lib/utils/date'

export default {
  data() {
    return {
      StatusOptions: [{
        value: '1',
        label: '待使用'
      }, {
        value: '2',
        label: '已使用'
      }, {
        value: '3',
        label: '已锁定'
      }, {
        value: '4',
        label: '已过期'
      }],
      searchform: {
        Keywords: '',
        NickName: '',
        Status: null
      },
      loadBtnFunc: [],
      tableData: [],
      tableloading: true,
      pagination: {
        currentPage: 1,
        pagesize: 10,
        pageTotal: 0
      },
      sortableData: {
        order: '',
        sort: ''
      },
      formLabelWidth: '80px',
      currentId: '',
      currentSelected: []
    }
  },
  created() {
    this.pagination.currentPage = 1
    this.loadTableData()
  },
  methods: {
    loadTableData: function() {
      this.tableloading = true
      var seachdata = {
        'CurrentPage': this.pagination.currentPage,
        'length': this.pagination.pagesize,
        'Keywords': this.searchform.Keywords,
        'NickName': this.searchform.NickName,
        'Status': this.searchform.Status,
        'Order': this.sortableData.order,
        'Sort': this.sortableData.sort
      }
      getLogPage(seachdata).then(res => {
        this.tableData = res.ResData.Items
        this.pagination.pageTotal = res.ResData.TotalItems
        this.tableloading = false
      })
    },
    handleSearch: function() {
      this.pagination.currentPage = 1
      this.loadTableData()
    },
    /**
     * 当表格的排序条件发生变化的时候会触发该事件
     */
    handleSortChange: function(column) {
      this.sortableData.sort = column.prop
      if (column.order === 'ascending') {
        this.sortableData.order = 'asc'
      } else {
        this.sortableData.order = 'desc'
      }
      this.loadTableData()
    },
    /**
     * 当用户手动勾选checkbox数据行事件
     */
    handleSelectChange: function(selection, row) {
      this.currentSelected = selection
    },
    /**
     * 当用户手动勾选全选checkbox事件
     */
    handleSelectAllChange: function(selection) {
      this.currentSelected = selection
    },
    /**
     * 选择每页显示数量
     */
    handleSizeChange(val) {
      this.pagination.pagesize = val
      this.pagination.currentPage = 1
      this.loadTableData()
    },
    /**
     * 选择当页面
     */
    handleCurrentChange(val) {
      this.pagination.currentPage = val
      this.loadTableData()
    },
    dateformatter(row, column, cellValue, index) {
      var date = row[column.property]
      return cellValue ? fecha.format(new Date(date), 'yyyy-MM-dd') : ''
    },
    onInput() {
      this.$forceUpdate()
    }
  }
}
</script>
